﻿namespace QiangPiao
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows.Forms;

    public class FrmSplashScreen : Form
    {
        private IContainer components = null;
        private Font fnt = new Font("宋体", 12f, FontStyle.Bold);
        private LinkLabel lnkCtrol;
        private Panel pnl;
        private ProgressBar pross;
        private TextBox txtInfo;
        private string txtMesssage;

        public FrmSplashScreen()
        {
            this.InitializeComponent();
            base.Paint += new PaintEventHandler(this.FrmSplashScreen_Paint);
            this.txtMesssage = "正在检查版本...";
        }

        protected override void Dispose(bool disposing)
        {
            if(disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmSplashScreen_Paint(object sender, PaintEventArgs e)
        {
        }

        public void InitData()
        {
            try
            {
                this.SetMessage("正在检查版本...");
                //string version = PublicInfo.Proxy.GetVersion();
                //string str2 = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                //if(version != str2)
                //{
                //    this.SetMessage("发现新版本，[" + str2 + "]-[" + version + "]");
                //    Thread.Sleep(0x1388);
                //}
                //this.SetMessage("正在获取授权信息...");
                //if(!PublicInfo.GetAuthorizeInfo())
                //{
                //    this.SetProssVal(30);
                //    this.SetMessage("获取授权信息失败，请注册！");
                //    new FrmCheckAuthorize().ShowDialog();
                //}
                //if(PublicInfo.AuthorizeInfo != null)
                //{
                //this.SetProssVal(60);
                this.SetMessage("已成功取得授权，开始初始化！");
                this.fm = new FrmMain();
                this.SetProssVal(80);
                this.SetMessage("读取基础信息中...");
                this.fm.ReadData();
                this.SetProssVal(100);
                this.SetMessage("初始化已完成，正在启动中...");
                this.IsConnect = true;
                //}
                //else
                //{
                //    this.SetMessage("注册失败，系统即将关闭！");
                //    Thread.Sleep(0x7d0);
                //    this.IsConnect = false;
                //}
            }
            catch(Exception)
            {
                this.SetMessage("服务器连接异常，请检查网络连接。");
                Thread.Sleep(0x7d0);
                this.IsConnect = false;
            }
        }

        private void InitializeComponent()
        {
            this.pnl = new Panel();
            this.pross = new ProgressBar();
            this.txtInfo = new TextBox();
            this.lnkCtrol = new LinkLabel();
            base.SuspendLayout();
            this.pnl.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            //this.pnl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.pnl.BackColor = Color.Transparent;
            this.pnl.BackgroundImageLayout = ImageLayout.None;
            this.pnl.Location = new Point(9, 9);
            this.pnl.Margin = new Padding(0);
            this.pnl.Name = "pnl";
            this.pnl.Size = new Size(0x1bf, 0x1c);
            this.pnl.TabIndex = 0;
            this.pnl.Paint += new PaintEventHandler(this.pnl_Paint);
            this.pross.Location = new Point(12, 40);
            this.pross.Name = "pross";
            this.pross.Size = new Size(0x1b9, 0x17);
            this.pross.TabIndex = 1;
            this.txtInfo.Location = new Point(12, 0x54);
            this.txtInfo.Multiline = true;
            this.txtInfo.Name = "txtInfo";
            this.txtInfo.Size = new Size(0x1b9, 0x9e);
            this.txtInfo.TabIndex = 2;
            this.lnkCtrol.AutoSize = true;
            this.lnkCtrol.Location = new Point(0x1ab, 0x42);
            this.lnkCtrol.Name = "lnkCtrol";
            this.lnkCtrol.Size = new Size(0x1d, 12);
            this.lnkCtrol.TabIndex = 3;
            this.lnkCtrol.TabStop = true;
            this.lnkCtrol.Text = "详细";
            this.lnkCtrol.LinkClicked += new LinkLabelLinkClickedEventHandler(this.lnkCtrol_LinkClicked);
            base.ClientSize = new Size(0x1d1, 80);
            base.ControlBox = false;
            base.Controls.Add(this.lnkCtrol);
            base.Controls.Add(this.txtInfo);
            base.Controls.Add(this.pross);
            base.Controls.Add(this.pnl);
            this.DoubleBuffered = true;

            base.MaximizeBox = false;
            this.MaximumSize = new Size(0x1d7, 260);
            base.MinimizeBox = false;
            this.MinimumSize = new Size(0x1d7, 0x56);
            base.Name = "FrmSplashScreen";
            base.ShowInTaskbar = false;

            base.StartPosition = FormStartPosition.CenterScreen;
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void lnkCtrol_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(base.Height > 0x56)
            {
                base.Height = 0x56;
                this.lnkCtrol.Text = "详细";
            }
            else
            {
                base.Height = 260;
                this.lnkCtrol.Text = "收起";
            }
        }

        private void pnl_Paint(object sender, PaintEventArgs e)
        {
            StringFormat format;
            Rectangle clipRectangle = e.ClipRectangle;
            clipRectangle.Inflate(-1, -1);
            format = new StringFormat
            {
                Alignment = StringAlignment.Center
            };
            ControlPaint.DrawBorder3D(e.Graphics, clipRectangle, Border3DStyle.Adjust);
            Size size = e.Graphics.MeasureString(this.txtMesssage, this.fnt, 0x3e8, format).ToSize();
            clipRectangle.Width = size.Width + 1;
            clipRectangle.Height = size.Height + 1;
            clipRectangle.X = (this.pnl.Width - clipRectangle.Width) / 2;
            clipRectangle.Y = (this.pnl.Height - clipRectangle.Height) / 2;
            e.Graphics.DrawString(this.txtMesssage, this.fnt, new SolidBrush(Color.Black), clipRectangle, format);
        }

        private void SetMessage(string Message)
        {
            if(!this.pnl.InvokeRequired)
            {
                this.txtMesssage = Message;
                this.txtInfo.Text = this.txtInfo.Text + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss  ") + Message + "\r\n";
                this.txtInfo.SelectionStart = this.txtInfo.Text.Length - 1;
                this.pnl.Refresh();
            }
            else
            {
                this.pnl.Invoke(new dlgInvoke(this.SetMessage), new object[] { Message });
            }
        }

        private void SetProssVal(int val)
        {
            MethodInvoker method = null;
            if(!this.pross.InvokeRequired)
            {
                this.pross.Value = val;
            }
            else
            {
                if(method == null)
                {
                    method = () => this.SetProssVal(val);
                }
                this.pross.Invoke(method);
            }
        }

        public TimeSpan diffTime { get; protected set; }

        public FrmMain fm { get; set; }

        public bool IsConnect { get; set; }

        public string TxtMesssage
        {
            get
            {
                return this.txtMesssage;
            }
            set
            {
                this.txtMesssage = value;
            }
        }

        public delegate void dlgInvoke(string Message);
    }
}

